<?php
include 'secure/core.php';

$YOURPASSWORD   = $gateway->getPassword();

$password = $_REQUEST['password'];
if ($password != $YOURPASSWORD) {
    echo "Access Denied";
    exit;
}

/*
 * subid = the var which holds the conversion id and the random number
 * earn = how much the admin recieves for the offer
 * points  = the same as earn
 */

$subid   = $_REQUEST['subid'];
$earn    = $_REQUEST['earn'];
$pdtshow = $_REQUEST['pdtshow'];

$subid_parts = explode("n", $subid);
$conversion_id = $subid_parts[1];
$rand = $subid_parts[0];
	
$sql8 = "SELECT referral_percent, user_percent FROM `settings`";
$row8 = $ado->fetch($ado->exec($sql8));
$referral_percent= $row8['referral_percent'];
$user_percent = $row8['user_percent'];

/*
 * function below checks to see if conversion has already happened
 */

$sql1 = "select `id`, `user_id` from conversions where `id` = '$conversion_id' and `converted` = '1'";
$res1 = $ado->exec($sql1);
if($ado->count($res1) > 0)
{
	echo "The Same Conversion cannot be sent twice";
	exit;
}
	
/*
 * function below is updating the conversion to say its converted
 */
 
$now = date("Y-m-d H:i:s");
$sql2 = "update `conversions` set `converted` = '1', `time_converted` = '$now' where id='$conversion_id'";
$ado->exec($sql2);

/*
 * function below is getting information to credit the user
 */

$sql8 = "select `user_id`, `file_id` from conversions where `id` = '$conversion_id'";
$row11 = $ado->fetch($ado->exec($sql8));
$user_id = $row11['user_id'];
$file_id = $row11['file_id'];
$sql4 = "select `username` from `members` where `id` = '$user_id'";
$row4 = $ado->fetch($ado->exec($sql4));
$user_name = $row4['username']." (User ID: $user_id)";

/*
 * function below is getting the file information based on subid
 */

$sql5 = "select `file_title` from `uploads` where `string` = '$file_id'";
$row5 = $ado->fetch($ado->exec($sql5));
$user_string = $user_name." had an offer completed while downloading file: (".$row5['file_title']."). ";
$referal_string = $user_name." had an offer completed while downloading file: (".$row5['file_title']."). This added to the users referal earnings.";

/*
 * function below is telling the site how much to credit the user and getting the users referral id
 */

$credit_amount = round($earn * $user_percent, 2);
$admin_price = $earn;
$referrer_id = $row4['referrer_id'];

/*
 * function below is completing the process and adding the earnings to the user and or referral
 */

if($referrer_id == 0)
	{
		//enter the whole earning
		$sql7 = "insert into `earnings` set user_id = '$user_id', amount = '$credit_amount', date = '$now', file_id='$file_id'";
		$ado->exec($sql7);
	}
	else
	{
		$referral_credit = round($credit_amount * $referral_percentage, 2);
		$user_credit = round($credit_amount * (1 - $referral_percentage), 2);
		//give the percent commission to the referrer
		$sql9 = "insert into `earnings` set user_id = '$user_id', amount = '$user_credit', date = '$now', file_id='$string'";
		$ado->exec($sql9);
		
		//leave the rest for the user
		$sql10 = "insert into `earnings` set user_id = '$referrer_id', amount = '$referral_credit', date = '$now', file_id='$file_id'";
		$ado->exec($sql10);	
	}
	
/*
 * function below is adding the earnings to the admin column for tracking purposes
 */	
 
$sql10 = "insert into `site_earnings` set user_id = '$user_id', amount = '$admin_price', date = '$now'";
$ado->exec($sql10);

/*
 * function below is adding the download information and activating the download
 */
 
$sql10 = "insert into `downloads` set user_id = '$user_id', file_id = '$string', date = '$now'";
$ado->exec($sql10); 

$path = $settings->getUploadPath();
echo $files->download($path.$row5['file_title']);
?>